home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 216_01 / rbsb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  8.6 KB  |  325 lines

  1. /*% shar -v zmodem.h zm.c sz.c>/tmp/rzsz1.sh;shar -v rz.c rbsb.c rz.1 sz.1 gz>/tmp/rzsz2.sh; shar -v zmodem.h zm.c sz.c rz.c rbsb.c rz.1 sz.1 gz>/tmp/rzsz1et2.sh
  2.  *
  3.  * -rev 01-01-87
  4.  *  This file contains Unix specific stuff for setting terminal modes,
  5.  *  very little is specific to ZMODEM or YMODEM per se (that stuff is in
  6.  *  sz.c and rz.c).  The CRC-16 routines used by XMODEM, YMODEM, and ZMODEM
  7.  *  are also in this file, an iterative version, and a fast table driven macro
  8.  *  version, selected by #define CRCTABLE
  9.  *
  10.  *   This file is #included so the main file can set parameters such as HOWMANY.
  11.  *   See the main files (rz.c/sz.c) for compile instructions.
  12.  */
  13.  
  14. #ifdef V7
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sgtty.h>
  18. #define OS "V7/BSD"
  19. #endif
  20.  
  21. #ifndef OS
  22. #ifndef USG
  23. #define USG
  24. #endif
  25. #endif
  26.  
  27. #ifdef USG
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <termio.h>
  31. #include <sys/ioctl.h>
  32. #define OS "SYS III/V"
  33. #endif
  34.  
  35. #if HOWMANY  > 255
  36. Howmany must be 255 or less
  37. #endif
  38.  
  39. struct {
  40.     unsigned baudr;
  41.     int speedcode;
  42. } speeds[] = {
  43.     110,    B110,
  44.     300,    B300,
  45.     600,    B600,
  46.     1200,    B1200,
  47.     2400,    B2400,
  48.     4800,    B4800,
  49.     9600,    B9600,
  50.     19200,    EXTA,
  51.     9600,    EXTB,
  52.     0,
  53. };
  54.  
  55. int Twostop;        /* Use two stop bits */
  56.  
  57. static unsigned
  58. getspeed(code)
  59. {
  60.     register n;
  61.  
  62.     for (n=0; speeds[n].baudr; ++n)
  63.         if (speeds[n].speedcode == code)
  64.             return speeds[n].baudr;
  65.     return 0;
  66. }
  67.  
  68.  
  69.  
  70. #ifdef ICANON
  71. struct termio oldtty, tty;
  72. #else
  73. struct sgttyb oldtty, tty;
  74. struct tchars oldtch, tch;
  75. #endif
  76.  
  77. int iofd = 0;        /* File descriptor for ioctls & reads */
  78.  
  79. /*
  80.  * mode(n)
  81.  *  2: set a cbreak, XON/XOFF control mode if using Pro-YAM's -g option
  82.  *  1: save old tty stat, set raw mode 
  83.  *  0: restore original tty mode
  84.  */
  85. mode(n)
  86. {
  87.     static did0 = FALSE;
  88.  
  89.     vfile("mode:%d", n);
  90.     switch(n) {
  91. #ifdef USG
  92.     case 2:    /* Cbreak mode used by sb when -g detected */
  93.         if(!did0)
  94.             (void) ioctl(iofd, TCGETA, &oldtty);
  95.         tty = oldtty;
  96.  
  97.         tty.c_iflag = BRKINT|IXON;
  98.  
  99.         tty.c_oflag = 0;    /* Transparent output */
  100.  
  101.         tty.c_cflag &= ~PARENB;    /* Disable parity */
  102.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  103.         if (Twostop)
  104.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  105.  
  106. #ifdef XCLUDE
  107.         tty.c_lflag = XCLUDE | ISIG;
  108. #else
  109.         tty.c_lflag = ISIG;
  110. #endif
  111.  
  112.         tty.c_cc[VINTR] = Zmodem ? 03:030;    /* Interrupt char */
  113.         tty.c_cc[VMIN] = 1;
  114.  
  115.         (void) ioctl(iofd, TCSETAW, &tty);
  116.         did0 = TRUE;
  117.         return OK;
  118.     case 1:
  119.         if(!did0)
  120.             (void) ioctl(iofd, TCGETA, &oldtty);
  121.         tty = oldtty;
  122.  
  123.         tty.c_iflag = IGNBRK;
  124.  
  125.          /* No echo, crlf mapping, INTR, QUIT, delays, no erase/kill */
  126.         tty.c_lflag &= ~(ECHO | ICANON | ISIG);
  127. #ifdef XCLUDE
  128.         tty.c_lflag |= XCLUDE;
  129. #endif
  130.  
  131.         tty.c_oflag = 0;    /* Transparent output */
  132.  
  133.         tty.c_cflag &= ~PARENB;    /* Same baud rate, disable parity */
  134.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  135.         if (Twostop)
  136.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  137.         tty.c_cc[VMIN] = HOWMANY; /* This many chars satisfies reads */
  138.         tty.c_cc[VTIME] = 1;    /* or in this many tenths of seconds */
  139.         (void) ioctl(iofd, TCSETAW, &tty);
  140.         did0 = TRUE;
  141.         Baudrate = getspeed(tty.c_cflag & CBAUD);
  142.         return OK;
  143. #endif
  144. #ifdef V7
  145.     case 2:    /*  This doesn't work ... */
  146.         printf("No mode(2) in V7/BSD!"); bibi(99);
  147.         if(!did0) {
  148.             ioctl(iofd, TIOCEXCL, 0);
  149.             ioctl(iofd, TIOCGETP, &oldtty);
  150.             ioctl(iofd, TIOCGETC, &oldtch);
  151.         }
  152.         tty = oldtty;
  153.         tch = oldtch;
  154.         tch.t_intrc = Zmodem ? 03:030;    /* Interrupt char */
  155.         tty.sg_flags |= (ODDP|EVENP|CBREAK);
  156.         tty.sg_flags &= ~(ALLDELAY|CRMOD|ECHO|LCASE);
  157.         ioctl(iofd, TIOCSETP, &tty);
  158.         ioctl(iofd, TIOCSETC, &tch);
  159.         did0 = TRUE;
  160.         return OK;
  161.     case 1:
  162.         if(!did0) {
  163.             ioctl(iofd, TIOCEXCL, 0);
  164.             ioctl(iofd, TIOCGETP, &oldtty);
  165.             ioctl(iofd, TIOCGETC, &oldtch);
  166.         }
  167.         tty = oldtty;
  168.         tty.sg_flags |= RAW;
  169.         tty.sg_flags &= ~ECHO;
  170.         ioctl(iofd, TIOCSETP, &tty);
  171.         did0 = TRUE;
  172.         Baudrate = getspeed(tty.sg_ospeed);
  173.         return OK;
  174. #endif
  175.     case 0:
  176.         if(!did0)
  177.             return ERROR;
  178. #ifdef USG
  179.         (void) ioctl(iofd, TCSBRK, 1);    /* Wait for output to drain */
  180.         (void) ioctl(iofd, TCFLSH, 1);    /* Flush input queue */
  181.         (void) ioctl(iofd, TCSETAW, &oldtty);    /* Restore original modes */
  182.         (void) ioctl(iofd, TCXONC,1);    /* Restart output */
  183. #endif
  184. #ifdef V7
  185.         ioctl(iofd, TIOCSETP, &oldtty);
  186.         ioctl(iofd, TIOCSETC, &oldtch);
  187.         ioctl(iofd, TIOCNXCL, 0);
  188. #endif
  189.         return OK;
  190.     default:
  191.         return ERROR;
  192.     }
  193. }
  194.  
  195. sendbrk()
  196. {
  197. #ifdef V7
  198. #ifdef TIOCSBRK
  199. #define CANBREAK
  200.     sleep(1);
  201.     ioctl(iofd, TIOCSBRK, 0);
  202.     sleep(1);
  203.     ioctl(iofd, TIOCCBRK, 0);
  204. #endif
  205. #endif
  206. #ifdef USG
  207. #define CANBREAK
  208.     ioctl(iofd, TCSBRK, 0);
  209. #endif
  210. }
  211.  
  212. #ifdef FIONREAD
  213. #define READCHECK
  214. /*
  215.  *  Return non 0 iff something to read from io descriptor f
  216.  */
  217. rdchk(f)
  218. {
  219.     static long lf;
  220.  
  221.     ioctl(f, FIONREAD, &lf);
  222.     return ((int) lf);
  223. }
  224. #endif
  225. #ifdef SVR2
  226. #define READCHECK
  227. #include <fcntl.h>
  228.  
  229. char checked = '\0' ;
  230. /*
  231.  * Nonblocking I/O is a bit different in System V, Release 2
  232.  */
  233. rdchk(f)
  234. {
  235.     int lf, savestat = fcntl(f, F_GETFL) ;
  236.  
  237.     fcntl(f, F_SETFL, savestat | O_NDELAY) ;
  238.     lf = read(f, &checked, 1) ;
  239.     fcntl(f, F_SETFL, savestat) ;
  240.     return(lf) ;
  241. }
  242. #endif
  243.  
  244.  
  245. #ifdef CRCTABLE
  246. /* crctab calculated by Mark G. Mendel, Network Systems Corporation */
  247. static unsigned short crctab[256] = {
  248.     0x0000,  0x1021,  0x2042,  0x3063,  0x4084,  0x50a5,  0x60c6,  0x70e7,
  249.     0x8108,  0x9129,  0xa14a,  0xb16b,  0xc18c,  0xd1ad,  0xe1ce,  0xf1ef,
  250.     0x1231,  0x0210,  0x3273,  0x2252,  0x52b5,  0x4294,  0x72f7,  0x62d6,
  251.     0x9339,  0x8318,  0xb37b,  0xa35a,  0xd3bd,  0xc39c,  0xf3ff,  0xe3de,
  252.     0x2462,  0x3443,  0x0420,  0x1401,  0x64e6,  0x74c7,  0x44a4,  0x5485,
  253.     0xa56a,  0xb54b,  0x8528,  0x9509,  0xe5ee,  0xf5cf,  0xc5ac,  0xd58d,
  254.     0x3653,  0x2672,  0x1611,  0x0630,  0x76d7,  0x66f6,  0x5695,  0x46b4,
  255.     0xb75b,  0xa77a,  0x9719,  0x8738,  0xf7df,  0xe7fe,  0xd79d,  0xc7bc,
  256.     0x48c4,  0x58e5,  0x6886,  0x78a7,  0x0840,  0x1861,  0x2802,  0x3823,
  257.     0xc9cc,  0xd9ed,  0xe98e,  0xf9af,  0x8948,  0x9969,  0xa90a,  0xb92b,
  258.     0x5af5,  0x4ad4,  0x7ab7,  0x6a96,  0x1a71,  0x0a50,  0x3a33,  0x2a12,
  259.     0xdbfd,  0xcbdc,  0xfbbf,  0xeb9e,  0x9b79,  0x8b58,  0xbb3b,  0xab1a,
  260.     0x6ca6,  0x7c87,  0x4ce4,  0x5cc5,  0x2c22,  0x3c03,  0x0c60,  0x1c41,
  261.     0xedae,  0xfd8f,  0xcdec,  0xddcd,  0xad2a,  0xbd0b,  0x8d68,  0x9d49,
  262.     0x7e97,  0x6eb6,  0x5ed5,  0x4ef4,  0x3e13,  0x2e32,  0x1e51,  0x0e70,
  263.     0xff9f,  0xefbe,  0xdfdd,  0xcffc,  0xbf1b,  0xaf3a,  0x9f59,  0x8f78,
  264.     0x9188,  0x81a9,  0xb1ca,  0xa1eb,  0xd10c,  0xc12d,  0xf14e,  0xe16f,
  265.     0x1080,  0x00a1,  0x30c2,  0x20e3,  0x5004,  0x4025,  0x7046,  0x6067,
  266.     0x83b9,  0x9398,  0xa3fb,  0xb3da,  0xc33d,  0xd31c,  0xe37f,  0xf35e,
  267.     0x02b1,  0x1290,  0x22f3,  0x32d2,  0x4235,  0x5214,  0x6277,  0x7256,
  268.     0xb5ea,  0xa5cb,  0x95a8,  0x8589,  0xf56e,  0xe54f,  0xd52c,  0xc50d,
  269.     0x34e2,  0x24c3,  0x14a0,  0x0481,  0x7466,  0x6447,  0x5424,  0x4405,
  270.     0xa7db,  0xb7fa,  0x8799,  0x97b8,  0xe75f,  0xf77e,  0xc71d,  0xd73c,
  271.     0x26d3,  0x36f2,  0x0691,  0x16b0,  0x6657,  0x7676,  0x4615,  0x5634,
  272.     0xd94c,  0xc96d,  0xf90e,  0xe92f,  0x99c8,  0x89e9,  0xb98a,  0xa9ab,
  273.     0x5844,  0x4865,  0x7806,  0x6827,  0x18c0,  0x08e1,  0x3882,  0x28a3,
  274.     0xcb7d,  0xdb5c,  0xeb3f,  0xfb1e,  0x8bf9,  0x9bd8,  0xabbb,  0xbb9a,
  275.     0x4a75,  0x5a54,  0x6a37,  0x7a16,  0x0af1,  0x1ad0,  0x2ab3,  0x3a92,
  276.     0xfd2e,  0xed0f,  0xdd6c,  0xcd4d,  0xbdaa,  0xad8b,  0x9de8,  0x8dc9,
  277.     0x7c26,  0x6c07,  0x5c64,  0x4c45,  0x3ca2,  0x2c83,  0x1ce0,  0x0cc1,
  278.     0xef1f,  0xff3e,  0xcf5d,  0xdf7c,  0xaf9b,  0xbfba,  0x8fd9,  0x9ff8,
  279.     0x6e17,  0x7e36,  0x4e55,  0x5e74,  0x2e93,  0x3eb2,  0x0ed1,  0x1ef0
  280. };
  281.  
  282. /*
  283.  * updcrc macro derived from article Copyright (C) 1986 Stephen Satchell. 
  284.  *  NOTE: First srgument must be in range 0 to 255.
  285.  *        Second argument is referenced twice.
  286.  * 
  287.  * Programmers may incorporate any or all code into their programs, 
  288.  * giving proper credit within the source. Publication of the 
  289.  * source routines is permitted so long as proper credit is given 
  290.  * to Stephen Satchell, Satchell Evaluations and Chuck Forsberg, 
  291.  * Omen Technology.
  292.  */
  293.  
  294. #define updcrc(cp, crc) ( crctab[((crc >> 8) & 255)] ^ (crc << 8) ^ cp)
  295.  
  296. #else
  297.  
  298. unsigned short updcrc();
  299.  
  300. /* update CRC */
  301. unsigned short
  302. updcrc(c, crc)
  303. register c;
  304. register unsigned crc;
  305. {
  306.     register count;
  307.  
  308.     for (count=8; --count>=0;) {
  309.         if (crc & 0x8000) {
  310.             crc <<= 1;
  311.             crc += (((c<<=1) & 0400)  !=  0);
  312.             crc ^= 0x1021;
  313.         }
  314.         else {
  315.             crc <<= 1;
  316.             crc += (((c<<=1) & 0400)  !=  0);
  317.         }
  318.     }
  319.     return crc;    
  320. }
  321. #endif
  322.  
  323. /* End of rbsb.c */
  324.  
  325.